package com.salim.leetcode.$150;

import java.math.BigDecimal;
import java.util.Stack;

public class EvaluateReversePolishNotation {
    //原先我用BigDecimal 但给的testcase不会超 所以直接运算
    public int evalRPN(String[] tokens) {
        Stack<String> strings = new Stack<>();
        for(int i=0;i<tokens.length;i++){
            if(tokens[i].equals("+")){
                int a = Integer.parseInt(strings.pop());
                int b = Integer.parseInt(strings.pop());
                strings.push(String.valueOf(b+a));
            }else if(tokens[i].equals("-")){
                int a = Integer.parseInt(strings.pop());
                int b = Integer.parseInt(strings.pop());
                strings.push(String.valueOf(b-a));
            }else if(tokens[i].equals("*")){
                int a = Integer.parseInt(strings.pop());
                int b = Integer.parseInt(strings.pop());
                strings.push(String.valueOf(b*a));
            }else if(tokens[i].equals("/")){
                int a = Integer.parseInt(strings.pop());
                int b = Integer.parseInt(strings.pop());
                strings.push(String.valueOf(b/a));
            }else{
                strings.push(tokens[i]);
            }
        }
        return Integer.parseInt(strings.peek());
    }
}
